/*
 * Copyright 2001 Alexander Boverman and the original authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _WT_UTIL_H
#define _WT_UTIL_H

#include <tcl.h>
#include "WtContext.h"

char *WtToString(Tcl_Obj *obj);
Tcl_Obj *WtNewString(const char *val);
Tcl_Obj *WtNewBool(int val);
int WtUpdateTableFromAp(Tcl_Obj *tbl, table *apTable,
  Tcl_Interp *interp);
int WtUpdateTableFromApNoMulti(Tcl_Obj *tbl, table *apTable,
  Tcl_Interp *interp);
int WtUpdateTableObjFromAp(Tcl_Obj *tbl, table *apTable,
  Tcl_Interp *interp, int doMulti);
int WtUpdateApFromLVTable(table *apTable, Tcl_Obj *tbl,
  Tcl_Interp *interp);
int WtLog(const char *file, int line, int mask, WtContext *w,
  const char *fmt, ...);
int WtLogInternal(const char *file, int line, int mask, WtContext *w,
  const char *str, int addNotice);
int WtAddNotice(WtContext *w, char *notice, int mask, Tcl_Interp *interp);
int WtCopyOnWrite(Tcl_Obj **objPtr);
int WtEvalIncr(Tcl_Interp *interp, int objc, Tcl_Obj **objv, int flags);
Tcl_Obj *WtIncr(Tcl_Obj *obj);
Tcl_Obj *WtDecr(Tcl_Obj *obj);
Tcl_Obj **WtIncrList(Tcl_Obj **objv, int n);
Tcl_Obj **WtDecrList(Tcl_Obj **objv, int n);
Tcl_Obj *WtSetObj(Tcl_Obj **objPtr, Tcl_Obj *val);
int WtGetBoolResult(Tcl_Interp *interp, int *boolVal);
int WtCommandExists(Tcl_Interp *interp, const char *name);
char *WtSafeStr(const char *str);
Tcl_Obj *WtCmdGetVal(Tcl_Obj *val, Tcl_ObjType *type,
  Tcl_Interp *interp);
int WtCmdGetVar(Tcl_Obj *var, Tcl_Obj **val, Tcl_ObjType *type,
  Tcl_Interp *interp);
int WtCmdOwnVar(Tcl_Obj *var, Tcl_Obj **val, Tcl_ObjType *type,
  int *created, Tcl_Interp *interp);
Tcl_Obj *WtCmdSetVar(Tcl_Obj *var, Tcl_Obj *val, Tcl_Interp *interp);
void WtUpdateProtectedObjString(Tcl_Obj *obj);
int WtSetProtectedObjFromAny(Tcl_Interp *interp, Tcl_Obj *obj,
  Tcl_ObjType *type);
int WtNamespaceExport(Tcl_Interp *interp, const char *ns, const char *ptn);
unsigned int WtHashString(const char *str, int len);
char *WtEncodeUrl(const char *str, int stringLen, int *resultLen);
int WtHexToChar(char *p);
int WtDecodeUrlInPlace(char *str, int len);

extern const char *wtBadUsagePrefix;
extern const char *wtBadUsagePrefix2;

#define DEBUG1(str) (WtLog(##HERE, APLOG_DEBUG | APLOG_NOERRNO, \
                             WtGetContext(), (str)))

#define HERE __FILE__, __LINE__

#ifdef WIN32
#define random rand
#define srandom srand
#endif /* WIN32 */

#endif /* !_WT_UTIL_H */
